<?php
namespace App\Http\Controllers\H5\Shop;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Model\ShopAccount;
use App\Common\Model\ShopUserStatus;
use App\Common\Service\PassportService;
use App\Common\Service\ShopAccountService;
use App\Common\Tool\AxAes;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Log;


class PassportController extends Controller {


    /**
     *  demo
     */
    public function index(Request $request)
    {
        $code = $request->code;
        $type = $request->type;

        if ($type == 1) {
            echo $code . "加密后:" . AxAes::encrypt($code) ."<br/>";
        } else {
            echo $code . "解密后:" . AxAes::decrypt($code)."<br/>";
        }
    }

    public function get_code(Request $request, PassportService $passportService, ShopAccountService $accountService) {
        $mobile  = $request->mobile;
        $imgCode = $request->img_code;
        $refCode  = $request->ref_code;

        if( $accountService->ischeckRecommen($refCode) ) {
            throw new AixinException("对不起，您推荐码信息有误");
        }

        if(empty($mobile) || empty($imgCode)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $accountInfo = $accountService->isOldUser($mobile);
        if( empty($accountInfo) && empty($refCode) ) {
            throw new AixinException("{$mobile}：新用户没有推荐码!");
        }


        $isSend =  $passportService->sendcode($mobile, $imgCode);
        if(!$isSend) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        $data = [];

        return ApiResponse($data);

    }

    public function imgCode(Request $request, PassportService $passportService) {
        $mobile = $request->mobile;
        if(empty($mobile)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $imgRes = $passportService->getImgCode($mobile);
      ;
        $base64Img = base64_encode($imgRes);
        $data = [
            'imgcode_base64'=>$base64Img,
        ];

        return ApiResponse($data);

    }


    public function login(Request $request, PassportService $passportService, ShopAccountService $accountService) {

        $mobile = $request->mobile;
        $phoneCode = $request->send_code;
        $refCode  = $request->ref_code;

        if( $accountService->ischeckRecommen($refCode) ) {
            throw new AixinException("对不起，您推荐码信息有误");
        }

        $accountInfo = $accountService->isOldUser($mobile);
        Log::info("response accountInfo res：".json_encode($accountInfo));
        if( empty($accountInfo) && empty($refCode) ) {
            throw new AixinException(AixinExceptionCode::VCODE_INVALID, "{$mobile}：新用户没有推荐码!");
        }

        $isCheckPhoneCode = $passportService->isCheckPhoneCode($mobile, $phoneCode);
        if(!$isCheckPhoneCode) {
            throw new AixinException(AixinExceptionCode::VCODE_INVALID);
        }
        $uid = $passportService->getUniqueUid($mobile);
        if( empty($uid)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        $token = $passportService->getToken($uid);
        if( empty($token) ) {
            throw new AixinException(AixinExceptionCode::TOKEN_NULL);
        }

        //电商用户不存在
        if( empty($accountInfo) ) {

            $pUid = AxAes::decrypt($refCode);
            Log::info("添加新用户|推荐码{$refCode}:解密后".json_encode($pUid));
            $pUser = $accountService->userInfo($pUid);


            $id = $accountService->addNewUser($uid, $mobile, $pUid);
            if(empty($id)) {
                throw new AixinException(AixinExceptionCode::RESULT_NULL, "电商用户注册失败!");
            }
            //新用户注册给用户状态表里面写入一条数据
            $addUserStatus = $accountService->addUserStatus($uid);
            if(empty($addUserStatus)){
                throw new AixinException(AixinExceptionCode::RESULT_NULL,"新用户注册写入状态失败！");
            }
        }

        $shopUserStatus = $accountService->getUserStatus($uid);
        $workFlowCode = ShopUserStatus::USER_BASE_PAGE;     //默认填写基本信息
        if(!empty($shopUserStatus)) {
            $workFlowCode = $accountService->workFlow($shopUserStatus);
        }

        $data = [
            'uid'    =>   $uid,
            'token'  =>   $token,
            'recode' =>   $workFlowCode,
        ];


        return ApiResponse($data);


    }

    public function shopupdatemobile (Request $request, PassportService $passportService, ShopAccountService $shopAccountService) {
        $mobile = $request->mobile;
        $newMobile = $request->new_mobile;
        $phoneCode = $request->send_code;


        if (empty($mobile) || empty($newMobile) || empty($phoneCode)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $isCheckPhoneCode = $passportService->isCheckPhoneCode($mobile, $phoneCode);

        if(!$isCheckPhoneCode) {
             throw new AixinException(AixinExceptionCode::VCODE_INVALID);
        }

        $uid = $passportService->getUniqueUid($mobile);
        if( empty($uid)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        $accountInfo = $shopAccountService->userinfo($uid);
        if( empty($accountInfo) ) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }

        $isUpdate = $passportService->shopChangeMobile($uid, $newMobile);
        if( empty($accountInfo) ) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }

        return ApiResponse();

    }



}
